home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / setup.exe / {app} / asymptopia / Localizer_01.py < prev    next >
Encoding:
Python Source  |  2003-07-26  |  13.2 KB  |  317 lines

  1. """
  2. /***************************************************************************
  3.  
  4.     Author             :Charles B. Cosse 
  5.     
  6.     Email            :ccosse@asymptopia.com
  7.                     :ccosse@linux4schools.org
  8.                     
  9.     Copyright        :(C) 2001,2002 Asymptopia Software.
  10.     
  11.  ***************************************************************************/
  12. /***************************************************************************
  13.                           Localizer.py
  14.  
  15.     Description:
  16.     
  17.     THESE AREN'T SPOTS -- should they be? currently we're handling just 
  18.     localization before getting into the guest-exchange business; on the 
  19.     other hand, we do need to know if it's an operator, etc, and if we want 
  20.     to go with it, then if already a spot, then it's already there; BUT, since 
  21.     we're trying firstly to *substitute*, then we'd be having to put something
  22.     *back* as well as commit -- not a good way, hmmm. Best soln would be to 
  23.     know all the various ways we might go about making the dealer move and 
  24.     then make our requests to Tux accordingly, like 
  25.     "get_submission(<existing_spot_to_use_as_substitute>) -- this *does* get 
  26.     lengthly, so think we'll just go with the good-'ol 1-step-at-a-time and 
  27.     worry about next detail separately.
  28.  
  29.  ***************************************************************************/
  30.  
  31. /***************************************************************************
  32.  *                                                                         *
  33.  *   This program is free software; you can redistribute it and/or modify  *
  34.  *   it under the terms of the GNU General Public License as published by  *
  35.  *   the Free Software Foundation; either version 2 of the License, or     *
  36.  *   (at your option) any later version. (Please note that if you use this *
  37.  *   code you must give credit by including the Author and Copyright       *
  38.  *   info at the top of this file).                                        *
  39.  ***************************************************************************/
  40.  
  41. """
  42. import pygame,os
  43. from pygame.locals import *
  44.  
  45. class Localizer:
  46.     """Localizer has game model.
  47.     """
  48.     def __init__(self,board,game):
  49.         self.board=board
  50.         self.game=game#only used to update score
  51.         
  52.     def localize(self,submission):
  53.         #print 'Localizer.localize: submission=',submission
  54.         if len(submission)==0:return(None)
  55.         board=self.board
  56.         #if self.board.num_submissions==0:
  57.         if len(self.board.get_listofheads())==0:
  58.             for elem in submission:
  59.                 if elem.count('WC'):return(None)
  60.             #print 'zeroeth submission:',submission
  61.             lhs=board.N/2-len(submission)/2    #this can't work with floats!
  62.             row=board.M/2
  63.             rlist=[]
  64.             for col in range(len(submission)):
  65.                 rlist.append([submission[col],row,lhs+col])
  66.             
  67.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  68.             #head_spot stuff for zeroth submission (assuming only row here)
  69.             head_spot=board.get_spotMN(row,lhs)
  70.                     
  71.             head_spot.AMHEAD=1
  72.             head_spot.AMROWEXPR=1
  73.             head_spot.ROWEXPRLENGTH=len(submission)
  74.             self.game.tuxscore=self.game.tuxscore+len(submission)
  75.             return(rlist)    
  76.                 
  77.         else:
  78.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  79.             #print 'submission number:',board.num_submissions
  80.             spots=board.get_spots()
  81.             
  82.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  83.             #just print text version of board to terminal w/*'s at HEADS
  84.             for m in range(board.M):
  85.                 for n in range(board.N):
  86.                     for spot in spots:
  87.                         if spot.M==m and spot.N==n:
  88.                             if spot.guest and spot.AMHEAD:dummy=0#print '*%2s'%spot.guest.str_val,
  89.                             elif spot.guest:dummy=0#print '%3s'%spot.guest.str_val,
  90.                             else:dummy=0#print '%3s'%'x',
  91.                 #print ''
  92.             
  93.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  94.             #get the list_of_heads and print-out(list_of_heads==list of spots)
  95.             list_of_heads=board.get_listofheads()
  96.             for headspot in list_of_heads:
  97.                 #print headspot.guest.str_val
  98.                 pass
  99.             
  100.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  101.             #have to head-this-off if has wildcards: ("WC")
  102.             #if submission has WC, then that's all we try to satisfy
  103.             list_of_submission_indices=[]
  104.             for idx in range(len(submission)):
  105.                 if submission[idx][0:3]=='WC:':
  106.                     submission[idx]=submission[idx][3:]
  107.                     list_of_submission_indices.append(idx)
  108.                     #print 'list_of_submission_indices=',list_of_submission_indices,' submission->',submission
  109.             
  110.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  111.             #submission lacked WC, so we try to replace one from submission.
  112.             if len(list_of_submission_indices)==0:
  113.                 for idx in range(len(submission)):list_of_submission_indices.append(idx)
  114.             
  115.             #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  116.             
  117.             #print 'localizer:',list_of_submission_indices
  118.             
  119.             #now it's satisfying both matches of wc=wc separately -- ok remains "1", but fix[0] lost for fix[1]
  120.             for head_idx in range(len(list_of_heads)):
  121.                 #try column expression:
  122.                 row=list_of_heads[head_idx].M
  123.                 col=list_of_heads[head_idx].N
  124.                 
  125.                 ok=1
  126.                 for  dummy in range(len(list_of_submission_indices)):
  127.                     #"substitution_idx" is actually the idx of another list (i.e. the submission array)
  128.                     substitution_idx=list_of_submission_indices[dummy]
  129.                     #print 'substitution_idx=',substitution_idx
  130.                         
  131.                     #problem facing 2x substitution: 
  132.                 
  133.                     
  134.                     #print 'TRYING COL EXPR'
  135.                     
  136.                     #HERE! We are only searcing along 1 row. Should really just go by brute force w/however many WC's and
  137.                     #overlay; just only submit with WC's (that's Tux's unique, hardcoded restriction).
  138.                     
  139.                     head_spot=board.get_spotMN(row,col)
  140.                     if head_spot.AMROWEXPR==1:
  141.                         #print 'head_spot.ROWEXPRLENGTH:',head_spot.ROWEXPRLENGTH
  142.                         #print 'using HEAD at:',row,col
  143.                         self.current_submission_type='col'
  144.                         
  145.                         for col_idx in range(col,col+head_spot.ROWEXPRLENGTH):#property of head element only
  146.                             if col_idx>board.N-1:continue
  147.                             #print 'checking along row for substitution candidate: requesting spotMN(',row,',',col_idx,')'
  148.                             
  149.                             
  150.                             #HERE! Instead of checkinf for particular WC, check if _ANY_
  151.                             if board.get_spotMN(row,col_idx).guest.str_val==submission[substitution_idx]:#check-4-opportunity
  152.                             
  153.                                 if board.get_spotMN(row,col_idx).AMHEAD and board.get_spotMN(row,col_idx).AMCOLEXPR:#bug fix: we're traversing a row and chose a COLHEAD
  154.                                     #print 'bailing out -- too much effort required here!' 
  155.                                     continue
  156.                                 #how many empty above/below? vs. substitution_idx/len(submission)
  157.                                 num_needed_above=substitution_idx
  158.                                 num_needed_below=len(submission)-substitution_idx-1
  159.                                 #print submission[substitution_idx],num_needed_above,num_needed_below
  160.                                 
  161.                                 if row-num_needed_above<0:
  162.                                     ok=0
  163.                                     continue
  164.                                 if row+num_needed_below>board.M-1:
  165.                                     ok=0
  166.                                     continue
  167.                                 
  168.                                 #did not re-check logic latest use of this section
  169.                                 #could have board.check_if_occupied(m,n) and avoid exceptions...
  170.                                 for num_above in range(num_needed_above,0,-1):
  171.                                     if board.check4guest(row-num_above-1,col_idx):ok=0#above
  172.                                     if board.check4guest(row-num_above,col_idx-1):ok=0#lhs
  173.                                     if board.check4guest(row-num_above,col_idx+1):ok=0#rhs
  174.                                     if board.check4guest(row-num_above,col_idx):
  175.                                         if num_needed_above-num_above==substitution_idx:pass
  176.                                         else:ok=0#below
  177.                                 #print 'okabove=',ok,' submission=',submission
  178.                                 
  179.                                 for num_below in range(1,num_needed_below+1):
  180.                                     if board.check4guest(row+num_below+1,col_idx):ok=0#below
  181.                                     if board.check4guest(row+num_below,col_idx-1):ok=0#rhs
  182.                                     if board.check4guest(row+num_below,col_idx+1):ok=0#lhs
  183.                                     if board.check4guest(row+num_below,col_idx):
  184.                                         if num_needed_below-num_below == substitution_idx:pass#okay for substidx to be "fixed" (that's why we're using it!)
  185.                                         else:ok=0#above
  186.                                 #print 'okbelow=',ok,' submission=',submission
  187.             
  188.                                 #if subbing endpts, check either side of them:
  189.                                 if substitution_idx==0:
  190.                                     if row>0:
  191.                                         if board.check4guest(row-1,col_idx):ok=0
  192.                                 if substitution_idx==len(submission)-1:
  193.                                     if row+substitution_idx<=board.M-1:
  194.                                         if board.check4guest(row+substitution_idx,col_idx):ok=0
  195.                                 
  196.                                 if ok==1 and dummy==len(list_of_submission_indices)-1:
  197.                                     rlist=[]
  198.                                     for col in range(len(submission)):
  199.                                         
  200.                                         #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
  201.                                         #only doing last substitution idx
  202.                                         #instead of asking only "if current", need to see if it's _anywhere_ in list_of_substitution_indices
  203.                                         #if col != substitution_idx:
  204.                                         
  205.                                         #then still need to maintain "ok" over whole range of len(list_of_sub_idxs)
  206.                                         
  207.                                         if not list_of_submission_indices.count(col):
  208.                                             rlist.append([submission[col],row-num_needed_above+col,col_idx])
  209.                                         #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    
  210.                                     
  211.                                     #board.num_submissions=board.num_submissions+1
  212.                                     #print rlist
  213.                                     
  214.                                     #fill-in head-spot info:
  215.                                     if substitution_idx==0:
  216.                                         head_spot=board.get_spotMN(row,col_idx)
  217.                                     else:
  218.                                         head_spot=board.get_spotMN(row-num_needed_above,col_idx)
  219.                                     head_spot.AMHEAD=1
  220.                                     head_spot.AMCOLEXPR=1
  221.                                     head_spot.COLEXPRLENGTH=len(submission)
  222.                                     self.game.tuxscore=self.game.tuxscore+head_spot.COLEXPRLENGTH
  223.                                     #print 'rlist=',rlist
  224.                                     print 'returning from localizer:',rlist
  225.                                     return(rlist)
  226.             
  227.                     
  228.  
  229.             for head_idx in range(len(list_of_heads)):
  230.                 #try row expression:
  231.                 row=list_of_heads[head_idx].M
  232.                 col=list_of_heads[head_idx].N
  233.                 ok=1
  234.                 for dummy in range(len(list_of_submission_indices)):#see if board has this available to use/substitute<-should be "substitution_idx
  235.                     substitution_idx=list_of_submission_indices[dummy]
  236.                     #print 'substitution_idx=',substitution_idx
  237.                 
  238.                     #problem facing 2x substitution: 
  239.                 
  240.                     
  241.                     #print 'TRYING ROW EXPR'
  242.                     head_spot=board.get_spotMN(row,col)
  243.                     if head_spot.AMCOLEXPR==1:
  244.                         #print 'using HEAD at:',row,col
  245.                         #print 'AMCOLEXPR==1 for row,col=',row,col
  246.                         self.current_submission_type='row'
  247.                         for row_idx in range(row,row+head_spot.COLEXPRLENGTH):#property of head element only
  248.                             if row_idx>board.M-1:continue
  249.                             #print 'retrieving str_val at: ',row_idx,col,' to replace: ',submission[substitution_idx]
  250.                             if board.get_spotMN(row_idx,col).guest.str_val==submission[substitution_idx]:#check-4-opportunity
  251.                                 if board.get_spotMN(row_idx,col).AMHEAD and board.get_spotMN(row_idx,col).AMROWEXPR:#bug fix: we're traversing a col and chose a ROWHEAD
  252.                                     #print 'bailing out -- too much effort required here!' 
  253.                                     continue
  254.                                 #how many empty above/below? vs. substitution_idx/len(submission)
  255.                                 num_needed_left=substitution_idx
  256.                                 num_needed_right=len(submission)-substitution_idx-1
  257.                                 #print submission[substitution_idx],num_needed_left,num_needed_right
  258.  
  259.                                 #ok=1
  260.                                 if col-num_needed_left<0:
  261.                                     ok=0
  262.                                     continue
  263.                                 if col+num_needed_right>board.N-1:
  264.                                     ok=0
  265.                                     continue
  266.                                 
  267.                                 #did not re-check logic latest use of this section
  268.                                 #could have board.check_if_occupied(m,n) and avoid exceptions...
  269.                                 for num_left in range(num_needed_left,0,-1):
  270.                                     if board.check4guest(row_idx-1,col-num_left):ok=0#above
  271.                                     if board.check4guest(row_idx+1,col-num_left):ok=0#lhs
  272.                                     if board.check4guest(row_idx,col-num_left-1):ok=0#rhs
  273.                                     if board.check4guest(row_idx,col-num_left):
  274.                                         if num_needed_left-num_left==substitution_idx:pass
  275.                                         else:ok=0#below
  276.                                 #print 'okleft=',ok,' submission=',submission
  277.                                 
  278.                                 
  279.                                 for num_right in range(1,num_needed_right+1):
  280.                                     if board.check4guest(row_idx-1,col+num_right):ok=0#below
  281.                                     if board.check4guest(row_idx+1,col+num_right):ok=0#rhs
  282.                                     if board.check4guest(row_idx,col+num_right+1):ok=0#lhs
  283.                                     if board.check4guest(row_idx,col+num_right):
  284.                                         if num_needed_right-num_right == substitution_idx:pass#okay for substidx to be "fixed" (that's why we're using it!)
  285.                                         else:ok=0#above
  286.                                 #print 'okright=',ok,' submission=',submission
  287.                                 
  288.                                 #if subbing endpts, check either side of them:
  289.                                 if substitution_idx==0:
  290.                                     if col>0:
  291.                                         if board.check4guest(row_idx,col-1):ok=0
  292.                                 if substitution_idx==len(submission)-1:
  293.                                     if col+substitution_idx<=board.N-1:
  294.                                         if board.check4guest(row_idx,col+substitution_idx):ok=0
  295.                                                                 
  296.                                 if ok==1 and dummy==len(list_of_submission_indices)-1:
  297.                                     rlist=[]
  298.                                     for row in range(len(submission)):
  299.                                         #if row != substitution_idx:
  300.                                         if not list_of_submission_indices.count(row):
  301.                                             rlist.append([submission[row],row_idx,col-num_needed_left+row])
  302.                                     #board.num_submissions=board.num_submissions+1
  303.                                     #print rlist
  304.                                     
  305.                                     #fill-in head-spot info:
  306.                                     if substitution_idx==0:
  307.                                         head_spot=board.get_spotMN(row_idx,col)
  308.                                     else:
  309.                                         head_spot=board.get_spotMN(row_idx,col-num_needed_left)
  310.                                     head_spot.AMHEAD=1
  311.                                     head_spot.AMROWEXPR=1
  312.                                     head_spot.ROWEXPRLENGTH=len(submission)
  313.                                     self.game.tuxscore=self.game.tuxscore+head_spot.ROWEXPRLENGTH
  314.                                     #print 'rlist=',rlist
  315.                                     return(rlist)
  316.             return(None)                        
  317.